﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using OpenSoftStore.BusinessLogic.Member;
using OpenSoftStore.Client.Common;
using OpenSoftStore.Orm.Model;
using OpenSoftStore.Orm.View;

namespace OpenSoftStore.Client.UIL.ManageMember.Info
{
    public partial class Owed : UserControl
    {
        private static Owed instance;
        public MemberLogic MemberBll;
        public List<V_MemberOwed> MemberOweds;
        public T_Member Member;
        public static Owed Instance
        {
            get { return instance ?? (instance = new Owed()); }
        }

        public Owed()
        {
            InitializeComponent();
            MemberBll=new MemberLogic();
            Member=new T_Member();
            MemberOweds=new List<V_MemberOwed>();
            SPACommon.SetUiStyle(this);
        }

        public void GetOweds(T_Member member)
        {
            Member = member;
            MemberOweds = MemberBll.GetMemberOwedView(" where (NeedPay - CashPay - BankPay - ValuePay - FavorMoney - AllReturn - ReturnFavor) > 0 and MemberId= '" + member.Id + "'");
            dgvOwed.DataSource = MemberOweds;

            var memberReturnMoneys = MemberBll.GetMemberReturnMoneys(" MemberId = '" + member.Id + "'").ToList();
            dgvMemberReturnMoney.DataSource = memberReturnMoneys;
        }

        private void dgvMemberReturnMoney_SelectionChanged(object sender, EventArgs e)
        {
            if (dgvMemberReturnMoney.CurrentRow == null) return;
            var id = dgvMemberReturnMoney.CurrentRow.Cells["Id"].Value.ToString();

            dgvMemberReturnMoneyItem.AutoGenerateColumns = false;
            dgvMemberReturnMoneyItem.DataSource = MemberBll.GetMemberReturnMoneyItems(" ReturnId = '" + id + "'");
        }

        private void tlSave_Click(object sender, EventArgs e)
        {
            var memberReturnMoneyEditWindow = new MemberReturnMoneyEditWindow();
            memberReturnMoneyEditWindow.SetMember(Member);
            memberReturnMoneyEditWindow.ShowDialog();
        }
    }
}
